var express = require('express');
var router = express.Router();
let { departments_Model, Inspection_Model, InspectionTwo_Model } = require('../mongoose')

//当前时间
function times() {
    var myDate = new Date(); //创建Date对象
    var Y = myDate.getFullYear(); //获取当前完整年份
    var M = myDate.getMonth() + 1; //获取当前月份
    var D = myDate.getDate(); //获取当前日1-31
    var H = myDate.getHours(); //获取当前小时
    var i = myDate.getMinutes(); //获取当前分钟
    var s = myDate.getSeconds(); //获取当前秒数
    // 月份不足10补0
    if (M < 10) {
        M = '0' + M;
    }// 日不足10补0
    if (D < 10) {
        D = '0' + D;
    }// 小时不足10补0
    if (H < 10) {
        H = '0' + H;
    }// 分钟不足10补0
    if (i < 10) {
        i = '0' + i;
    }// 秒数不足10补0
    if (s < 10) {
        s = '0' + s;
    }// 拼接日期分隔符根据自己的需要来修改
    return Y + '-' + M + '-' + D + ' ' + H + ':' + i + ':' + s; function times() {
        var myDate = new Date(); //创建Date对象
        var Y = myDate.getFullYear(); //获取当前完整年份
        var M = myDate.getMonth() + 1; //获取当前月份
        var D = myDate.getDate(); //获取当前日1-31
        var H = myDate.getHours(); //获取当前小时
        var i = myDate.getMinutes(); //获取当前分钟
        var s = myDate.getSeconds(); //获取当前秒数
        // 月份不足10补0
        if (M < 10) {
            M = '0' + M;
        }// 日不足10补0
        if (D < 10) {
            D = '0' + D;
        }// 小时不足10补0
        if (H < 10) {
            H = '0' + H;
        }// 分钟不足10补0
        if (i < 10) {
            i = '0' + i;
        }// 秒数不足10补0
        if (s < 10) {
            s = '0' + s;
        }// 拼接日期分隔符根据自己的需要来修改
        return Y + '-' + M + '-' + D + ' ' + H + ':' + i + ':' + s;
    }
}

//添加科室列表
router.post('/addDepartment', async (req, res) => {
    let body = req.body;
    await departments_Model.create(body)
    res.send('科室添加成功')
});

//展示科室列表
router.get('/departmentlist', async (req, res) => {
    let list = await departments_Model.find().lean()
    let data = []
    let listObj = {}
    list.forEach(item => listObj[item._id] = item)
    list.forEach(item => (!item['top_id']) ? (data.push(item)) : (!listObj[item['top_id']]['children']) ? (listObj[item['top_id']]['children'] = []) : (listObj[item['top_id']]['children'].push(item)))
    res.send(data)
});


//检查报告添加
router.post('/addInspection', async (req, res) => {
    let body = req.body;
    await Inspection_Model.create(body)
    res.send({ code: 201, msg: '添加成功' })
})
//检查报告展示 提供用户id即可
router.post('/inspection/:id', async (req, res) => {
    let list = await Inspection_Model.find({ top_id: req.params.id })
    res.send({ code: 201, list: list })
})
//检验报告添加***************
router.post('/addInspectionTwo', async (req, res) => {
    let body = req.body;
    body['endTime'] = times()
    await InspectionTwo_Model.create(body)
    res.send({ code: 201, msg: '添加成功' })
})
//检验报告展示*************** 提供用户id即可
router.post('/inspectionTwo/:id', async (req, res) => {
    let list = await InspectionTwo_Model.find({ top_id: req.params.id })
    res.send({ code: 201, list: list })
})


module.exports = router;
